package org.itboys.kitchen.service.mongo;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.kitchen.entity.mongo.KitchenExpressMode;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.springframework.stereotype.Service;


/**
 * 作者：jiangxiong
 * 日期：2016年11月25日
 * 联系方式 ：empty90911@126.com
 * 描述：豪意运费模板-service
 */
@Service
public class KitchenExpressModeService extends BaseMongoService<KitchenExpressMode> {
	
	
	private static final long serialVersionUID = 2415524731052541775L;
	@Resource(name="kitchenDs")
	private MongoDataSource haoyiDataSource;
	@Override
	protected MongoDataSource getMongoDataSource() {
		return haoyiDataSource;
	}

	@Override
	protected Class<KitchenExpressMode> getEntityClass() {
		return KitchenExpressMode.class;
	}
	//保存

	/**
	 * 新增或保持
	 * @param expressMode
	 * @throws IOException
	 */
	public void saveMode(KitchenExpressMode expressMode) {
		//校验数据
		check(expressMode);
		if(expressMode.getId()==0l){
			super.save(expressMode);
		}else{
			super.updateExceptEmpty(expressMode.getId(), expressMode);
		}
		
	}
	//查询
	public PageResult<KitchenExpressMode> select(HttpServletRequest request){
		Map<String,String> containsparam =new HashMap<String,String>();
		Map<String,Object> param =new HashMap<String, Object>();
		return  super.containpageQuery(request,containsparam, param);
	}
	//校验
	public void check(KitchenExpressMode expressMode){
		//查看原来是否有模板存在
		Map<String,Object> param =new HashMap<String, Object>();
		param.put("isDeleted",0);
		if(expressMode.getId()==0l&&ListUtil.isNotNull(super.list(param))){
			throw new ErrorException("运费模板有且只能存在一个");
		}
		//校验模板1
		Double baseAmount1 = expressMode.getBaseAmount1();
		Double baseAmount2 = expressMode.getBaseAmount2();
		Double baseAmount3 = expressMode.getBaseAmount3();
		Double expressAmount1 = expressMode.getExpressAmount1();
		Double expressAmount2 = expressMode.getExpressAmount2();
		Double expressAmount3 = expressMode.getExpressAmount3();
		if(baseAmount1>=baseAmount2||
				baseAmount1>=baseAmount3||
				baseAmount2>=baseAmount3){
			throw new ErrorException("起送价格，需要依次递增，且不相等");
		}
		/*if(expressAmount1<expressAmount2||
				expressAmount1<expressAmount3||
						expressAmount2<expressAmount3){
			throw new ErrorException("配送费，需要依次递减");
		}*/
	
	}
}
